﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zor_IDAL;
using Zor_Tools;

namespace Zor_DAL
{
    public class PayTaxesBrowse_DAL<T> where T : class
    {
        /// <summary>
        /// 跟据登录的部门ID,获取社区名称
        /// </summary>
        /// <returns></returns>
        public static List<T> SelectCommunity()
        {
            int Dtid = RequestSession.GetSessionUser().DT_Id;
            int Isadmin = RequestSession.GetSessionUser().Is_Admin;
            var sql = "";

            if (Isadmin == 1 || Dtid == 16)
            {
                //税务局、管理员
                sql = "select[Dt_Id],[Dt_Name] from [dbo].[Sys_Department] where Dt_Level=4 ";
            }
            else
            {
                var sqlStr = "select[Dt_Id],[Dt_Name] from [dbo].[Sys_Department] where Dt_ParentId= " + Dtid + "";
                List<dynamic> Dt_IdLst = DapperQuery<dynamic>.DoQueryList(sqlStr, null);
                //判断当前登录用户是街道办还是税务所
                if (Dt_IdLst.Count == 1)
                {
                    //税务所
                    sql = "select[Dt_Id],[Dt_Name] from [dbo].[Sys_Department] where Dt_ParentId = (select Dt_Id from [Sys_Department] where Dt_ParentId =" + Dtid + ") ";

                }
                else
                {
                    //街道办
                    sql = "select[Dt_Id],[Dt_Name] from [dbo].[Sys_Department] where Dt_ParentId= " + Dtid + "";
                }                                 
            }         
            return DapperQuery<T>.DoQueryList(sql, null);
        }

        /// <summary>
        /// 跟据登录的部门ID,获取街道办名称
        /// </summary>
        /// <returns></returns>
        public static List<T> SelectStreetOffice()
        {
            int Dtid = RequestSession.GetSessionUser().DT_Id;
            int Isadmin = RequestSession.GetSessionUser().Is_Admin;
            var sql = "";

            if (Isadmin == 1 || Dtid == 16)
            {
                //税务局、管理员
                sql = "select[Dt_Id],[Dt_Name] from [dbo].[Sys_Department] where Dt_Level=3 ";
            }
            else            
            {
                var sqlStr = "select[Dt_Id],[Dt_Name] from [dbo].[Sys_Department] where Dt_ParentId= " + Dtid + "";
                List<dynamic> Dt_IdLst = DapperQuery<dynamic>.DoQueryList(sqlStr, null);
                //判断当前登录用户是街道办还是税务所
                if (Dt_IdLst.Count == 1)
                {
                    //税务所
                    sql = "select[Dt_Id],[Dt_Name] from [dbo].[Sys_Department] where Dt_Id = (select Dt_Id from [Sys_Department] where Dt_ParentId =" + Dtid + ") ";

                }
                else
                {
                    //街道办
                    sql = "select[Dt_Id],[Dt_Name] from [dbo].[Sys_Department] where Dt_Id= " + Dtid + "";
                }                 
            }
            return DapperQuery<T>.DoQueryList(sql, null);
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public static List<T> GetPayTaxesBrowseList(string where = null)
        {
            string strwhere = "";
            if (!string.IsNullOrEmpty(where))
            {
                strwhere = "1=1 and" + where;
            }
            var sql = "select * from [V_PayTaxesBrowse] where " + strwhere + " order by Pt_Date  desc ";
            return DapperQuery<T>.DoQueryList(sql, null);
        }
    }
}
